from __future__ import annotations

from datetime import datetime, timedelta, timezone
from typing import Dict, List

from .events import index_by_id, read_owner_events
from .notif_cache import get_notifications_within


def _parse_iso(s: str) -> datetime:
    try:
        if s.endswith("Z"):
            s = s[:-1] + "+00:00"
        return datetime.fromisoformat(s)
    except Exception:
        return datetime.now(timezone.utc)


def list_notifications(memory_dir, owner: str, for_id: str, last_hours: int = 24) -> List[Dict[str, str]]:
    """Return derived notifications using a small local cache for speed."""
    return get_notifications_within(memory_dir, owner, for_id, last_hours)
